//
//  WDJsonManager.m
//  WDPolymerizeSDK
//
//  Created by little People on 2023/6/14.
//

#import "WDJsonManager.h"
#import "WDNSLog.h"

@implementation WDJsonManager
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
{
    if (jsonString == nil) {
        return @{};
    }

    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err)
    {
        WDLog(@"json解析失败：%@",err);
        return @{};
    }
    return dic;
}

+ (NSString *)shortVersionString
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    
    // 2.app版本
    NSString *applocalversion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    return applocalversion;
}

+ (NSString *)BMSVersionString
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    
    // 2.app版本
    NSString *applocalversion = [infoDictionary objectForKey:@"BMSVersion"];
    return applocalversion;
}

// 读取本地JSON文件
+ (NSDictionary *)readSDKConfigFile {
//    NSString *bundlePath = [[NSBundle bundleForClass:[self class]].resourcePath stringByAppendingPathComponent:@"/WDSource.bundle"];
//    NSString *filePath = [NSString stringWithFormat:@"%@/sdk_config.json",bundlePath];
    
    //获取文件路径
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sdk_config" ofType:@"json"];
    
    //获取文件内容
    NSString *jsonStr  = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    //将文件内容转成数据
    NSData *jaonData   = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    //将数据转成数组
    NSDictionary *dic= [NSJSONSerialization JSONObjectWithData:jaonData options:NSJSONReadingMutableContainers error:nil];
    return dic;
}

+ (NSString *)adjustTokenForSDKConfigFile
{
    NSDictionary *dict = [self readSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"ajConfig"];
    return [NSString stringWithFormat:@"%@",configDict[@"token"]];
}

+ (NSString *)thAppidForSDKConfigFile
{
    NSDictionary *dict = [self readSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"thConfig"];
    return [NSString stringWithFormat:@"%@",configDict[@"app_id"]];
}

+ (NSString *)thServerUrlForSDKConfigFile
{
    NSDictionary *dict = [self readSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"thConfig"];
    return [NSString stringWithFormat:@"%@",configDict[@"server_url"]];
}

+ (NSString *)requestAppNameForSDKConfigFile
{
    NSDictionary *dict = [self readSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"requestConfig"];
    return [NSString stringWithFormat:@"%@",configDict[@"app_name"]];
}

+ (NSString *)inAppPruchaseAdjustEventKeyForSDKConfigFile
{
    NSDictionary *dict = [self readSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"gpConfig"];
    NSString *eventName = [[configDict allKeys] containsObject:@"adjust_event_key"]?[NSString stringWithFormat:@"%@",configDict[@"adjust_event_key"]]:@"";
    return eventName;
}

+ (NSString *)isAppKeyForSDKConfigFile
{
    NSDictionary *dict = [self readSDKConfigFile];
    return [NSString stringWithFormat:@"%@",dict[@"isAppKey"]];
}


+ (NSString *)maxKeyForSDKConfigFile
{
    NSDictionary *dict = [self readSDKConfigFile];
    return [NSString stringWithFormat:@"%@",dict[@"maxKey"]];
}
+ (NSString *)maxRewardForSDKConfigFile
{
    NSDictionary *dict = [self readSDKConfigFile];
    return [NSString stringWithFormat:@"%@",dict[@"maxReward"]];
}
+ (NSString *)maxInterForSDKConfigFile
{
    NSDictionary *dict = [self readSDKConfigFile];
    return [NSString stringWithFormat:@"%@",dict[@"maxInter"]];
}
+ (NSString *)maxBannerForSDKConfigFile
{
    NSDictionary *dict = [self readSDKConfigFile];
    return [NSString stringWithFormat:@"%@",dict[@"maxBanner"]];
}




+ (NSArray *)consumableProductIdForSDKConfigFile
{
    NSMutableArray *tempArray = [NSMutableArray array];
    NSDictionary *dict = [self readSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"gpConfig"];
    NSArray *consumableIds = configDict[@"consumableIds"];
    if (consumableIds.count <= 0)
    {
        return @[];
    }
    for (int i = 0; i < consumableIds.count; i++) {
        NSDictionary *dict = consumableIds[i];
        NSString *productId = dict[@"productId"];
        [tempArray addObject:productId];
    }
    return tempArray;
}

+ (NSArray *)nonConsumableProductIdForSDKConfigFile
{
    NSMutableArray *tempArray = [NSMutableArray array];
    NSDictionary *dict = [self readSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"gpConfig"];
    NSArray *nonConsumableIds = configDict[@"nonConsumableIds"];
    if (nonConsumableIds.count <= 0)
    {
        return @[];
    }
    for (int i = 0; i < nonConsumableIds.count; i++) {
        NSDictionary *dict = nonConsumableIds[i];
        NSString *productId = dict[@"productId"];
        [tempArray addObject:productId];
    }
    
    return [NSArray arrayWithArray:tempArray];
}

+ (NSArray *)subscribeProductIdForSDKConfigFile
{
    NSMutableArray *tempArray = [NSMutableArray array];
    NSDictionary *dict = [self readSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"gpConfig"];
    NSArray *subscribeIds = configDict[@"subscribeIds"];
    if (subscribeIds.count <= 0)
    {
        return @[];
    }
    for (int i = 0; i < subscribeIds.count; i++) {
        NSDictionary *dict = subscribeIds[i];
        NSString *productId = dict[@"productId"];
        [tempArray addObject:productId];
    }
    
    return [NSArray arrayWithArray:tempArray];
}

+ (NSArray *)productIdListForSDKConfigFile
{
    NSMutableArray *tempArray = [NSMutableArray array];
    NSDictionary *dict = [self readSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"gpConfig"];
    NSArray *consumableIds = configDict[@"consumableIds"];
    if (consumableIds.count > 0)
    {
        for (int i = 0; i < consumableIds.count; i++) {
            NSDictionary *dict = consumableIds[i];
            NSString *productId = dict[@"productId"];
            [tempArray addObject:productId];
        }
    }
    
    NSArray *nonConsumableIds = configDict[@"nonConsumableIds"];
    if (nonConsumableIds.count > 0)
    {
        for (int i = 0; i < nonConsumableIds.count; i++) {
            NSDictionary *dict = nonConsumableIds[i];
            NSString *productId = dict[@"productId"];
            [tempArray addObject:productId];
        }
    }
    
    
    NSArray *subscribeIds = configDict[@"subscribeIds"];
    if (subscribeIds.count > 0)
    {
        for (int i = 0; i < subscribeIds.count; i++) {
            NSDictionary *dict = subscribeIds[i];
            NSString *productId = dict[@"productId"];
            [tempArray addObject:productId];
        }
    }
    
    
    return [NSArray arrayWithArray:tempArray];
}

+ (NSString *)renvenueWithProductId:(NSString *)productId
{
    NSMutableArray *tempArray = [NSMutableArray array];
    NSDictionary *dict = [self readSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"gpConfig"];
    NSArray *consumableIds = configDict[@"consumableIds"];
    NSArray *nonConsumableIds = configDict[@"nonConsumableIds"];
    NSArray *subscribeIds = configDict[@"subscribeIds"];
    if (consumableIds.count > 0)
    {
        [tempArray addObjectsFromArray:consumableIds];
    }
    if (nonConsumableIds.count > 0)
    {
        [tempArray addObjectsFromArray:nonConsumableIds];
    }
    if (subscribeIds.count > 0)
    {
        [tempArray addObjectsFromArray:subscribeIds];
    }
    
    if (tempArray.count <= 0)
    {
        return @"";
    }
    
    for (int i = 0; i < tempArray.count; i++) {
        NSDictionary *dict = tempArray[i];
        NSString *pid = dict[@"productId"];
        NSString *dollar_price = [NSString stringWithFormat:@"%@",dict[@"dollar_price"]];
        if ([pid isEqualToString:productId])
        {
            return dollar_price;
        }
    }
    
    return @"";
}

@end
